<template>
	<view class="content">
		<view class="chapterView" v-for="(item,index) in chapterList" :key='index'>
			<view class="chapterItem" @tap="gotoChild(item.zid,index)">
				<view class="fname">{{item.section_name}}</view>
				<i class="iconfont iconxiala" :class="item.isShow?'i_active':''"></i>
			</view>
			<view class="childItem" v-show="item.isShow" v-for="(item1,index1) in item.subList" :key='index1'>
				<view class="zname">{{item1.section_name}}</view>
				<view><text class="checkButton" @tap="checkQuestion(item.section_name,item1.section_name,item1.zid)">查看试题</text></view>
			</view>
		</view>
		<uni-load-more  v-if="chapterList.length > 12" :status="status" :content-text="contentText" />
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default{
		data() {
			return {
				sid: '',
				subName: '',
				textbook: '',
				grade: '',
				education: 6,
				chapterList: [],
				page: 1,
				status: 'more',
				reload: true,
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多',
					contentnot: "没数据"
				}
			}
		},
		components:{
			uniLoadMore
		},
		onPullDownRefresh() {
			this.reload = true;
			this.page = 1;
			this.getChapter();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			this.status = 'loading';
			this.page = this.page+1;
			var _this = this;
			setTimeout(function(){
				_this.getChapter();
			},500);
		},
		onLoad(e) {
			this.sid = e.sid;
			this.subName = e.subject_name;
			this.textbook = e.textbook;
			this.grade = uni.getStorageSync('currentChild').grade;
			if(this.grade <= 6) {
				this.education = 6;
			} else if(this.grade > 6 && this.grade <= 9) {
				this.education = 9;
			} else if(this.grade > 9 && this.grade <= 12) {
				this.education = 9;
			}
			uni.setNavigationBarTitle({
				title: this.subName
			})
			this.getChapter();
		},
		methods:{
			getChapter(){
				var data = {
					grade: this.grade,
					education: this.education,
					textbook: this.textbook,
					sid: this.sid,
					page: this.page,
					pagesize: 13,
					type: 1
				}
				this.$zapi.apiPost('user/subject.Lists/sectionList',data).then(res=>{
					if(res.data.code == 200) {
						var list = res.data.data.data;
						for(var i = 0; i < list.length; i++) {
							list[i].isShow = false;
							list[i].subList = [];
						}
						this.chapterList = this.reload ? list : this.chapterList.concat(list);
						this.reload = false;
						if(res.data.data.data.length > 0) {
							this.status='more';
						} else {
							this.status='noMore';
						}
					} else {
						if(this.reload) {
							this.chapterList = [];
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						} else {
							this.status = 'noMore';
						}
					}
				})
			},
			gotoChild(fid,index) {
				this.chapterList[index].isShow = !this.chapterList[index].isShow;
				var data = {
					grade: this.grade,
					education: this.education,
					textbook: this.textbook,
					sid: this.sid,
					pagesize: 99,
					type: 2,
					fid: fid
				}
				this.$zapi.apiPost('user/subject.Lists/sectionList',data).then(res=>{
					if(res.data.code == 200) {
						this.chapterList[index].subList = res.data.data.data;
					} else {
						this.chapterList[index].subList = [];
					}
				})
				this.$forceUpdate();
			},
			checkQuestion(fname,zname,zid) {
				uni.navigateTo({
					url: 'question?sid='+this.sid+'&textbook='+this.textbook+'&zid='+zid+'&name='+fname+'&zname='+zname
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #FAFAFA;
	}
	.chapterView{
		padding: 0 30upx;
		width: 690upx;
		background-color: #FFFFFF;
		flex-direction: column;
	}
	.chapterItem{
		height: 100upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.fname{
		color: #333333;
		font-size: 36upx;
	}
	.i_active{
		transform: rotate(180deg);
	}
	.childItem{
		height: 90upx;
		border-bottom: 2upx solid #E5E5E5;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.zname{
		color: #333333;
		font-size: 32upx;
		width: 75%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	.checkButton{
		color: #FFFFFF;
		padding: 12upx 18upx;
		line-height: 1;
		background-color: #FF9900;
		border-radius: 46upx;
	}
</style>
